package com.iedu.demo.lgq.mapper;

import com.iedu.demo.lgq.entity.ChatMessage;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Date;

@Mapper
public interface ChatMessageMapper {

    // 插入一条聊天记录
    @Insert("INSERT INTO chat_messages (sender, receiver, content, timestamp) " +
            "VALUES (#{sender}, #{receiver}, #{content}, #{timestamp})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insertChatMessage(ChatMessage chatMessage);

    // 查询用户和商家之间的聊天记录
    @Select("SELECT * FROM chat_messages " +
            "WHERE (sender = #{sender} AND receiver = #{receiver}) OR " +
            "(sender = #{receiver} AND receiver = #{sender}) " +
            "ORDER BY timestamp ASC")
    List<ChatMessage> findChatHistory(@Param("sender") String sender, @Param("receiver") String receiver);

    // 查询未读消息
    @Select("SELECT * FROM chat_messages " +
            "WHERE receiver = #{receiver} AND timestamp > #{lastTimestamp} " +
            "ORDER BY timestamp ASC")
    List<ChatMessage> findUnreadMessages(@Param("receiver") String receiver, @Param("lastTimestamp") Date lastTimestamp);
}